//模拟弹窗
bowlder.define(function(){
    var scope = this, $$ = bowlder;
    var noFixed = $$.msie < 7;
    var $window = $$(window);

    var state = scope.state = {
        maxWidth: 1050,
        staticHeight: 0,
        closeDelay: 0,
        mask: true,
        hideOnMask: 1
    };
    var cls = state.cls = {
        active: 'bd-popbox-active',
        mask: 'bd-popbox-mask'
    };

    scope.init = function($box){
        $box.css({
            position : noFixed ? 'absolute' : 'fixed'
        }).hide();
        var $mask;
        if(state.mask) $mask = $$("<div>").addClass(cls.mask).css({
            position: 'absolute',
            left: 0,
            top: 0,
            width: '100%'
        }).appendTo(document.body);

        function getContentSize(){
            var winSize = getWinSize();
            var winWidth = winSize.width;
            var winHeight =  winSize.height;
            var staticHeight = state.staticHeight;
            var width = $box[0].offsetWidth,
                height = $box[0].offsetHeight;
            var ratio = width / height;
            width = Math.min(state.maxWidth, winWidth);
            if(state.keepRatio){
                width = Math.min(width, (winHeight-staticHeight) * ratio);
                height = width/ratio + staticHeight;
            }
            $box.css({width: width});
            return {
                width: width,
                height: height
            };
        }
        function refreshPos(){ //重新定位
            var winSize = getWinSize();
            var winWidth = winSize.width;
            var winHeight = winSize.height;
            var sTop = noFixed ? document.body.scrollTop || document.documentElement.scrollTop : 0;
            var size = getContentSize();
            $box.css({
                left: Math.max((winWidth - size.width) / 2, 0),
                top: Math.max(sTop + (winHeight - size.height) / 2, 0)
            });
            $mask && $mask.css({
                height: Math.max(window.innerHeight, document.documentElement.offsetHeight, document.body.offsetHeight, document.documentElement.scrollHeight, document.body.scrollHeight)
            });
        };
        var _refresh = $$.throttle(refreshPos, 80);
        function pop(data){
            if($$.isObject(data)){
                $$.extend(scope, data);
            }
            if($box.hasClass(cls.active)){
                refreshPos();
                return;
            }
            $box.show();
            refreshPos();
            $box.addClass(cls.active);
            $window.bind("resize", _refresh);
            scope.$emit("onpop", $box);
            $mask && $mask.show();
            scope.$emit("poped", $box);
        };
        function close(){
            $box.removeClass(cls.active);
            setTimeout(function(){
                $box.hide();
                $mask && $mask.hide();
            }, state.closeDelay);
            scope.$emit("closed", $box);
            $window.unbind("resize", _refresh);
        };
        function getWinSize() {
            var pageWidth = window.innerWidth,
                pageHeight = window.innerHeight;
            if(typeof pageWidth != "number") {
                if(document.compatMode == "CSS1Compat") {
                    pageWidth = document.documentElement.clientWidth;
                    pageHeight = document.documentElement.clientHeight;
                } else {
                    pageWidth = document.body.clientWidth;
                    pageHeight = document.body.clientHeight;
                }
            }
            return {
                width: pageWidth,
                height: pageHeight
            };
        }

        scope.$on("pop", pop);
        scope.$on("close", close);
        scope.$on("refresh", refreshPos);
        $box.bind("click", function(e){
            var target = e.target;
            while(target && target != this){
                if($$.dom.hasRole(target, "popbox-close")){
                    e.stopPropagation();
                    close();
                    return false;
                }
                target = target.parentNode;
            }
        });
        if($mask && state.hideOnMask){
            $mask.bind("click", close);
        }
    };
});
